extends KinematicBody2D

onready var sprites_ani = $Sprites/SpritesAni
onready var player_area_2d = $PlayerArea2D
onready var timer = $Timer

# 敌人战斗属性
var max_hp # 最大生命值
var dmg = 1 # 碰到玩家伤害

var now_hp # 当前生命值

# 敌人属性
var move_speed = 400
var velocity = Vector2.ZERO

var cell_pos = Vector2() #在tilemap 的坐标

func _ready():
	sprites_ani.play("move")
	pass # Replace with function body.

func _physics_process(delta):
	move_state(delta)
	pass
	
func update_cell_position():
	#保存瓦片位置，以减少涉及的计算，缺点是每次炸弹更换单元格时都必须调用此函数
	cell_pos = GdGlobal.UIBattle.world_to_map(self.get_position())
	
func set_pos_and_update(abs_pos):
	#设置绝对位置并更新离散瓦片位置
	set_position(abs_pos)
	update_cell_position()
	
func move_state(delta):
	
	look_at( GdGlobal.UIBattle.ui_player.position)
	var input_vector = Vector2.ZERO
	input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
	input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
	input_vector = GdGlobal.UIBattle.ui_player.position-self.position 
	input_vector = input_vector.normalized() * move_speed * 0.5 * GdGlobal.TILE_SIZE * delta
	velocity = move_and_slide(input_vector)


func _on_PlayerArea2D_body_entered(body):
#	print(body)
	if body.get_groups().has("player"):
		body.player_hurn(10)
		timer.start()
		player_area_2d.set_deferred("monitoring",false)

func _on_Timer_timeout():
	player_area_2d.set_deferred("monitoring",true)
	pass # Replace with function body.
